package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.RoomInfo;
import com.atguigu.lease.model.entity.ViewAppointment;
import com.atguigu.lease.web.app.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.app.mapper.RoomInfoMapper;
import com.atguigu.lease.web.app.mapper.ViewAppointmentMapper;
import com.atguigu.lease.web.app.service.ViewAppointmentService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentDetailVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentItemVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【view_appointment(预约看房信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@RequiredArgsConstructor
@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment> implements ViewAppointmentService {

    private final ViewAppointmentMapper viewAppointmentMapper;

    private final ApartmentInfoMapper apartmentInfoMapper;

    private final RoomInfoMapper roomInfoMapper;

    @Override
    public List<AppointmentItemVo> listAppointmentItemVo() {
        return viewAppointmentMapper.listAppointmentItemVo();
    }

    // 写得很啰嗦待改善
    @Override
    public AppointmentDetailVo getAppointmentDetailVoById(Long id) {
        ViewAppointment viewAppointment = viewAppointmentMapper.selectById(id);
        if (null == viewAppointment){
            return null;
        }
        List<RoomInfo> roomInfos = roomInfoMapper.selectList(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, viewAppointment.getApartmentId()));
        if (null == roomInfos){
            return null;
        }
        ApartmentItemVo apartmentItemVo = apartmentInfoMapper.getApartmentItemVoByRoomId(roomInfos.get(0).getId());
        AppointmentDetailVo appointmentDetailVo = new AppointmentDetailVo();
        BeanUtils.copyProperties(viewAppointment,appointmentDetailVo);
        appointmentDetailVo.setApartmentItemVo(apartmentItemVo);
        return appointmentDetailVo;
    }
}




